// packageE/appointment/store/AppointmentManage/AppointmentManage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: "",
    keyword: "",
    classTab: [],
    listData: [],

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0 //总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    this.setData({
      keyword: "",
      active: "",
      listData: []
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getClass();
  },
  async del(e) {
    let id = e.currentTarget.dataset.id;
    let {
      data
    } = await app._getNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project.store-project-delete"),
      data: {
        id: id
      }
    });
    if (!data.result) {
      app.tips(data.msg);
      return
    }
    app.tips(data.msg);
    this.init();
    this.getData();
  },
  onchange(e) {
    this.setData({
      active: e.detail.name,
    })
    this.init();
    this.getData();
  },
  onsearch(e) {
    console.log(e)
    this.setData({
      keyword: e.detail
    })
    this.init();
    this.getData();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  init() {
    this.setData({
      isLoadMore: true,
      total_page: 0,
      page: 1
    })
  },
  async getClass() {
    let {
      data
    } = await app._getNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project-category.get-list")
    });
    console.log(data)
    if (data.result == 1) {
      this.setData({
        classTab: data.data,
      })
      let all = {
        id: '',
        name: '全部'
      }
      this.data.classTab.unshift(all)
      this.setData({
        classTab: this.data.classTab
      })
      this.getData();
    } else {
      app.tips(data.msg)
    }
  },  
  async getData() {
    let json = {
      search: {}
    }
    json.search.category_id = this.data.active ? this.data.active : "";
    json.search.title = this.data.keyword;
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project.store-project-list"),
      data: json,
    });
    this.data.isLoadMore = true;
    if (data.result == 1) {
      this.setData({
        listData: data.data.data,
        total_page: data.data.last_page
      })
    } else {
      this.data.isLoadMore = false;
      app.tips(data.msg)
    }
  },
  edit(e) {
    let {
      id,
      creator_id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/packageE/appointment/store/porjectEdit/porjectEdit?category_id=' + creator_id + '&id=' + id,
    })
  },
  toAddProject() {
    wx.navigateTo({
      url: '/packageE/appointment/store/porjectEdit/porjectEdit',
    })
  },
  async getMoreData() {
    this.data.isLoadMore = false; // 防止重复触发
    if (this.data.page >= this.data.total_page) {
      return
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        search: {},
        page: this.data.page
      };
      json.search.category_id = this.data.active;
      json.search.title = this.data.keyword;
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.appointment.frontend.project.store-project-list"),
        data: json,
      });
      this.data.isLoadMore = true;
      if (!data.result) {
        this.setData({
          page: this.data.page - 1,
          isLoadMore: false
        })
        return
      }
      let nextData = data.data.data;
      this.data.listData = this.data.listData.concat(nextData);
      this.setData({
        listData: this.data.listData
      })
    }
  }
})